﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Globalization;

namespace ResourceBlender_Express {
    public class Comparers {
        /// <summary>
        /// Compares entries in a dictionary by name.
        /// </summary>
        public class DictionaryValueComparer : IComparer<DictionaryEntry> {
            CaseInsensitiveComparer comp = new CaseInsensitiveComparer();
            public int Compare(DictionaryEntry x, DictionaryEntry y) {
                return comp.Compare(x.Value, y.Value);
            }
        }

        /// <summary>
        /// Compares language codes by their equivalent English names
        /// </summary>
        public class LanguageNameComparer : IComparer<string> {
            CaseInsensitiveComparer comp = new CaseInsensitiveComparer();
            public int Compare(string language, string otherLanguage) {
                string name1 = null,
                       name2 = null;
                try {
                    name1 = CultureInfo.GetCultureInfo(language).EnglishName;
                } catch {
                    name1 = language;
                }
                try {
                    name2 = CultureInfo.GetCultureInfo(otherLanguage).EnglishName;
                } catch {
                    name2 = language;
                }
                return comp.Compare(name1, name2);
            }
        }

        /// <summary>
        /// Compares language codes by their equivalent English names
        /// </summary>
        public class LanguageNameDictionaryComparer : IComparer<DictionaryEntry> {
            private CompareMode mode;
            public enum CompareMode {
                Keys,
                Values
            }
            public LanguageNameDictionaryComparer(CompareMode mode)
                : base() {
                this.mode = mode;
            }

            CaseInsensitiveComparer comp = new CaseInsensitiveComparer();
            public int Compare(DictionaryEntry entry1, DictionaryEntry entry2) {
                string code1,
                       englishName1 = null,
                       code2,
                       englishName2 = null;
                if(mode == CompareMode.Keys) {
                    code1 = entry1.Key.ToString();
                    code2 = entry2.Key.ToString();
                } else {
                    code1 = entry1.Value.ToString();
                    code2 = entry2.Value.ToString();
                }
                try {
                    englishName1 = CultureInfo.GetCultureInfo(code1).EnglishName;
                } catch {
                    englishName1 = code1;
                }
                try {
                    englishName2 = CultureInfo.GetCultureInfo(code2).EnglishName;
                } catch {
                    englishName2 = code2;
                }
                return comp.Compare(englishName1, englishName2);
            }
        }

        /// <summary>
        /// Compares the equality of strings.
        /// </summary>
        public class StringEqualityComparer : IEqualityComparer<string> {
            public bool Equals(string x, string y) {
                return x.Equals(y, StringComparison.InvariantCultureIgnoreCase);
            }
            public int GetHashCode(string obj) {
                return obj.GetHashCode();
            }
        }
    }
}
